package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Configuration
@Slf4j
public class Userjob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每日工单量列表初始化
     * @param param
     * @return
     * @throws Exception
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{
        //todo:查询用户列表
        List<UserEntity> userList = userService.list();
        int plusDays = "0".equals(param) ? 0 : 1;
        //todo:构建数据（zset）
        //大key
        for (UserEntity userEntity : userList) {
            if (userEntity.getRoleId() != null){
                String key = VMSystem.REGION_TASK_KEY_PREF + LocalDate.now().plusDays(plusDays).format(DateTimeFormatter.ofPattern("yyyyMMdd")) +
                        "." + userEntity.getRegionId() + "." + userEntity.getRoleCode();
                redisTemplate.opsForZSet().add(key, userEntity.getId(), 0);
            }
        }
        return ReturnT.SUCCESS;
    }
}
